"""
给回测函数使用，返回各指标多日数据，针对单个股票
"""
from ylTDX2 import * #from ylTDX import all def function
from  _utinity import *


def zjjr_I(df):    #资金介入
         
    OPEN=tdx(df["Open"].to_list()) #open 是file open
    HIGH=tdx(df["High"].to_list())
    LOW=tdx(df["Low"].to_list())
    CLOSE=tdx(df["Close"].to_list())
    VOL=tdx(df["Volume"].to_list())
    AMOUNT=tdx(df["Amount"].to_list())
    VARC=LOW
    VARD=REF(LOW,1)
    VARE=SMA(ABS(VARC-VARD),3,1)/SMA(MAX(VARC-VARD,0),3,1)*100
    VARF=MA(VARE/10,3)
    VAR10=LLV(LOW,30)
    VAR11=HHV(VARF,30)
    主力观望=MA(IF(LOW<=VAR10,(VARF+VAR11*2)/2,0),3)
    VAR1B=REF((LOW+OPEN+CLOSE+HIGH)/4,1)
    VAR2B=SMA(ABS(LOW-VAR1B),13,1)/SMA(MAX(LOW-VAR1B,0),10,1)
    VAR3B=EMA(VAR2B,10)
    VAR4B=LLV(LOW,33)
    VAR5B=EMA(IF(LOW<=VAR4B,VAR3B,0),3)*3
    主力进场=IF(VAR5B>REF(VAR5B,1),VAR5B,0) #main arg
    洗盘=IF(VAR5B<REF(VAR5B,1),VAR5B,0)
    VAR1=REF((LOW+OPEN+CLOSE+HIGH)/4,1)
    VAR2=SMA(ABS(LOW-VAR1),13,1)/SMA(MAX(LOW-VAR1,0),10,1)
    VAR3=EMA(VAR2,10)
    VAR4=LLV(LOW,33)
    VAR5=EMA(IF(LOW<=VAR4,VAR3,0),3)
    铁峰参数=VAR5>REF(VAR5,4)
    VAR2C=(HIGH+LOW+CLOSE*2)/4
    VAR3C=EMA(VAR2C,7)
    VAR4C=STD(VAR2C,7)
    VAR5C=(VAR2C-VAR3C)*100/VAR4
    VAR6C=EMA(VAR5C,3)
    WW=(EMA(VAR6C,5)+100)/2-3
    MM=HHV(WW,3)
    AAA=AMOUNT/VOL/100
    return 主力进场 
    
def zjxc_I(df):    #庄家吸筹
    OPEN=tdx(df["Open"].to_list()) #open 是file open
    HIGH=tdx(df["High"].to_list())
    LOW=tdx(df["Low"].to_list())
    CLOSE=tdx(df["Close"].to_list())
    VOL=tdx(df["Volume"].to_list())
    AMOUNT=tdx(df["Amount"].to_list())
    # Today=time.strftime("%Y-%m-%d-%H-%M-%S")
    YEAR=datetime.datetime.today().year
    # MONTH=int(Today.split('-')[1])
    MONTH=datetime.datetime.today().month
    M=54
    N=34 
    LC=REF(CLOSE,1)
    RSI=((SMA(MAX((CLOSE - LC),0),3,1) / SMA(ABS((CLOSE - LC)),3,1)) * 100)
    FF=EMA(CLOSE,3)
    MA15=EMA(CLOSE,21)
    TRI_ANGLE=CROSS(85,RSI) #其实调用RSI.__downcross__(85)
    VAR1=1
    VAR2=REF(LOW,1)*VAR1
    VAR3=SMA(ABS(LOW-VAR2),3,1)/SMA(MAX(LOW-VAR2,0),3,1)*100*VAR1
    VAR4=EMA(IF(CLOSE*1.3,VAR3*10,VAR3/10),3)*VAR1
    VAR5=LLV(LOW,30)*VAR1
    VAR6=HHV(VAR4,30)*VAR1
    VAR7=IF(MA(CLOSE,58),1,0)*VAR1
    VAR8=EMA(IF(LOW<=VAR5,(VAR4+VAR6*2)/2,0),3)/618*VAR7*VAR1  #main arg
    吸筹=IF(VAR8>100,100,VAR8)*VAR1
    庄家吸筹=IF(吸筹>-150,0,吸筹)
    散户=(HHV(HIGH,M)-CLOSE)/(HHV(HIGH,M)-LLV(LOW,M))*100
    RSV=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100
    K=SMA(RSV,3,1)
    D=SMA(K,3,1)
    J=K*3-D*2
    庄家=EMA(J,6)
    第一买点=CROSS(14,吸筹)  #main arg
    第二买点=CROSS(庄家,散户)  #main arg
    第一卖点=CROSS(88,庄家)  #main arg
    第二卖点=CROSS(散户,庄家)  #main arg
    return VAR8

def cdsz_I(df):    #抄底神针
    CLOSE=tdx(df["Close"].to_list())
    LC=REF(CLOSE,1)
    抄=SMA(MAX(CLOSE-LC,0),12,1)/SMA(ABS(CLOSE-LC),12,1)*100 #底=30,神=70,针=50 #main arg
    return 抄

def dkcw_I(df):    #多空仓位
    OPEN=tdx(df["Open"].to_list()) #open 是file open
    HIGH=tdx(df["High"].to_list())
    LOW=tdx(df["Low"].to_list())
    CLOSE=tdx(df["Close"].to_list())
    VOL=tdx(df["Volume"].to_list())
    AMOUNT=tdx(df["Amount"].to_list())

    VAR2=IF(HIGH==CLOSE,CLOSE*1.1,HIGH)
    VAR3=IF(HIGH==CLOSE,CLOSE*0.9,LOW)
    短期值1=EMA((HIGH+LOW)/2,5)
    顶部线1=EMA(EMA(VAR2,6)*1.05,8)
    底部线1=EMA(EMA(VAR3,6)*0.97,8)
    VARA1=EMA(CLOSE,5)
    VARB1=EMA(CLOSE,10)
    VARC1=EMA(CLOSE,20)
    VARD1=EMA(CLOSE,50)
    VARE1=(VARA1+VARB1+VARC1+VARD1)/4
    天线1=MA(VARE1,4)   
    # 天线2=ADD(MA(VARE1,5),multiply(MA(VARE1,5),0.03))
    天线3=MA(VARE1,4)-MA(VARE1,4)*0.03# 天线3<天线1 ,等距 MA(VARE1,4)*0.03
    RED_GREEN=IF(天线1>REF(天线1,1),1,2)#1 RED 2 GREEN
    空=REF(CLOSE,1)>REF(天线3,1) & CLOSE<天线3
    多=REF(CLOSE,1)<REF(天线1,1) & CLOSE>天线1
    空1=TFILTER(多,空,2) #用法:TFILTER(买入条件,卖出条件,N);过滤掉买入(卖出)信号发出后,下一个反向信号发出前的所有买入(卖出)信号,
                        # N=1表示仅对买入信号过滤;
                        # N=2表示仅对卖出信号过滤;
                        # N=0表示对买入和卖出信号都过滤,返回1,2表示买入或卖出条件成立;
                        # 同一K线上只能有一个信号;
    多1=TFILTER(多,空,1)
    VAR11=1
    VAR12=IF(HIGH==CLOSE,CLOSE*1.1,HIGH)*VAR11
    VAR13=IF(HIGH==CLOSE,CLOSE*0.9,LOW)*VAR11
    长期值2=EMA((HIGH+LOW)/2,5)
    顶部线2=EMA(EMA(VAR12,6)*1.05,8)
    底部线2=EMA(EMA(VAR13,6)*0.97,8)
    VAR18=(HIGH+LOW+CLOSE)/3
    VARA2=EMA(CLOSE,5)
    VARB2=EMA(CLOSE,10)
    VARC2=EMA(CLOSE,20)
    VARD2=EMA(CLOSE,50)
    VARE2=(VARA2+VARB2+VARC2+VARD2)/4
    天线12=EMA(VARE2,60)
    MAGENTA_BLUE=IF(天线12>REF(天线12,1),3,4) #3 MAGENTA ,4 BLUE

    duokongcangwei=[]
    for i in range(len(RED_GREEN)):#粉+红=满仓，粉+绿=六成，蓝+红=四成，蓝+绿=空仓
        if(RED_GREEN[i]==1 and MAGENTA_BLUE[i]==3):
            duokongcangwei.append(1)
        elif(RED_GREEN[i]==2 and MAGENTA_BLUE[i]==3):
            duokongcangwei.append(0.6)
        elif(RED_GREEN[i]==1 and MAGENTA_BLUE[i]==4):
            duokongcangwei.append(0.4)
        else:
            duokongcangwei.append(0)
    return duokongcangwei

if __name__=='__main__':
    df=get_data(799,N=60)
    draw_(df,cdsz_I(df),dkcw_I(df))
